package com.shen.thread.state;

public class TestStop implements Runnable {
    private boolean flag = true;//设置一个标志位

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("run" + i++);
        }
    }

    //设置一个公开方法停止线程，转换标志位
    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) {
        TestStop test = new TestStop();
        new Thread(test).start();

        for (int i = 0; i < 100; i++) {
            System.out.println("main" + i);
            if (i == 90) {
                test.stop();//调用stop切换标志位
                System.out.println("停止");
            }
        }
    }
}
